<?php

declare (strict_types=1);

namespace app\model\auth;

use app\common\model\Admin as AdminModel;
use app\common\model\AuthRole;
use app\common\model\AuthRoleAccess;

class Admin
{

    /**
     * 获取权限组列表
     *
     * @param array $childrenGroupIds 当前管理员所拥有权限的管理员
     *
     * @return array
     */
    public function getAuthGroupList(array $childrenGroupIds): array
    {
        $list = AuthRole::whereIn('id', $childrenGroupIds)
            ->field('id, pid, name')
            ->select()
            ->toArray();
        return $list;
    }

    /**
     * 根据角色id获取角色名称
     *
     * @param array $childrenRoleIds 子角色id
     *
     * @return array
     */
    public function getRoleName(array $childrenRoleIds): array
    {
        return AuthRole::whereIn('id', $childrenRoleIds)->column('id,name');
    }

    /**
     * 根据管理角色id，获取被分配的管理员id
     *
     * @param array $childrenGroupIds 子管理员分配的角色id
     *
     * @return array
     */
    public function getAuthRoleList(array $childrenGroupIds): array
    {
        $data = AuthRoleAccess::whereIn('group_id', $childrenGroupIds)
            ->field('uid, group_id')
            ->select()
            ->toArray();
        return $data;
    }

    /**
     * 获取管理员数据列表
     *
     * @param array $childrenAdminIds 子管理员id
     * @param array $where 查询条件
     * @param array $sort 排序
     * @param array $offset 分页
     *
     * @return array
     */
    public function getAdminList(array $childrenAdminIds, array $where, array $sort, array $offset)
    {
        // 获取数据数量
        $total = AdminModel::where($where)
            ->whereIn('id', $childrenAdminIds)
            ->count();
        // 获取数据列表
        $list = AdminModel::where($where)
            ->whereIn('id', $childrenAdminIds)
            ->withoutField(['password', 'token'])
            ->page($offset['offset'], $offset['limit'])
            ->order($sort['sort'], $sort['order'])
            ->order('status')
            ->select();
        return [$total, $list];
    }
}
